﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <link href="~/Content/lib/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/lib/layui/layui.js"></script>
</head>
<body>

    <form class="layui-form" action="">
        <input type="hidden" value="@ViewBag.pid" id="productId" />
        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block">
                <input type="file" id="my_file" name="file">
                <input type="text" id="image" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
                <img id="my_img" style="width:200px;height:200px" />
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script>
        //Demo
        layui.use(['form', 'upload'], function () {
            var form = layui.form;
            var upload = layui.upload;
            var $ = layui.$;
            upload.render({
                elem: "#my_file",
                url: "/Back/Home/UploadImg",
                done: function (res) {
                    if (res.code == 0) {
                        $("#my_img").attr("src", "/images/" + res.path);
                        $("#image").val(res.path);
                    }
                }
            })
            //监听提交
            form.on('submit(formDemo)', function (data) {
                $.ajax({
                    url: "/Back/Img/DealAdd",
                    type: "post",
                    data: {
                        image: $("#image").val(),
                        product_id: $("#productId").val()
                        /*product_id: $("#product_id").val(),
                        create_user: $("#create_user").val(),
                        create_time: $("#create_time").val(),
                        update_user: $("#update_user").val(),
                        update_time: $("#update_time").val()*/

                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.suc) {
                            layer.alert(res.msg);
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        } else {
                            layer.alert(res.msg);
                            window.parent.location.reload();
                        }
                    }
                })
                return false;
            });
        });
    </script>
</body>
</html>
